<template>
	<div class="userBg">
		
		<div class="container">
			<u-subsection :list="list" :current="current" @change="changeSub" fontSize="14" activeColor="#f46c6c"></u-subsection>
			<div class="question">
				<h3 v-if="showList.subTitle">选择求助题型</h3>
				<ul class="item">
					<li v-for="(item,j) in showList.subTitle" :key="j" :class="showList.selectTitle == item ? 'cur':''" @click="showList.selectTitle= item">{{ item }}</li>
				</ul>
				<div class="item">
					<u--textarea v-model="showList.answer" placeholder="请输入您的问题" maxlength="-1"></u--textarea>
				</div>
				<div class="item">
					<u-button type="error" text="提交问题" @click="subPj"></u-button>
				</div>
			</div>
		</div>
		
		
		<div class="loadBox" v-if="searchLoading">
			<u-loading-icon size="36" mode="circle" :vertical="true" color="red" text="加载中..." textSize="20"></u-loading-icon>
		</div>
	</div>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	const db = wx.cloud.database()
	export default {
		data() {
			return {
				userinfo:"",
				list:["按题型求助","按问题求助"],
				current:0,
				question:[
					{
						title:"按题型求助",
						subTitle:["人际关系","应急应变","综合分析","计划组织","自我认知与岗位匹配"],
						selectTitle:"",
						answer:""
					},
					{
						title:"按问题求助",
						answer:""
					}
				],
				searchLoading:true
			}
		},
		onLoad(options) {
			
		},
		computed: {
			userphone() {
				return this.$store.getters.userphone ? this.$store.getters.userphone : '';
			},
			showList(){
				let that = this
				return that.question[that.current]
			}
		},
		created() {
			let that = this;
			that.getUserinfo()
		},
		mounted() {
			let that = this 
			
		},
		watch: {
			
		},
		methods: {
			...mapActions(['LogSetPhone','LogSetOpenid']),
			getUserinfo(){
				let that = this
				db.collection('24gk_tkzinfo').where({phone: that.userphone}).get({
					success:res=>{
						that.userinfo = res.data[0]
						that.searchLoading = false
					}
				})
			},
			changeSub(e){
				this.current = e
			},
			getTime(){
				let that = this
				let day = new Date()
				let y = day.getFullYear()
				let m = (day.getMonth() + 1).toString().padStart(2, '0');
				let d = day.getDate().toString().padStart(2, '0');
				let hour = day.getHours().toString().padStart(2, '0');
				let miu = day.getMinutes().toString().padStart(2, '0');
				let sec = day.getSeconds().toString().padStart(2, '0');
				let time = y + '-' + m + '-' + d + ' ' + hour + ':' + miu + ':' + sec
				return time
			},
			subPj(){
				let that = this
				let subObj = {
					user: that.userinfo.name,
					phone:that.userinfo.phone,
					banji: that.userinfo.banji,
					photo: that.userinfo.photo,
					title:that.showList.title,
					selectTitle:that.showList.selectTitle ? that.showList.selectTitle : '',
					answer:that.showList.answer,
					time: that.getTime()
				}
				if(!subObj.answer){
					uni.showToast({
						title:"请输入求助问题",
						icon:"error"
					})
					return
				}
				that.searchLoading = true
				db.collection('qiuzhu_questions').add({
					data:subObj,
					success: res=>{
						uni.showToast({
							title:"您已提交，后续会为您解决",
							icon:'success'
						})
						that.searchLoading = false
					},
					fail:req=>{
						that.searchLoading = false
					}
				})
			}
		},
		components: {
			
		}
	};
</script>

<style scoped>
	@import url(./index.css);
</style>